package com.zhenyi.jiahua.task;

import java.util.List;

import org.joda.time.DateTime;

import com.jfinal.kit.LogKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.SqlPara;
import com.jiahua.common.model.JhStaff;
import com.utils.DateUtils;
import com.zhenyi.jiahua.core.utils.MessageUtils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.StrFormatter;

public class JHBirthTask implements Runnable {

	@Override
	public void run() {
		int day = 30;
		String date = DateUtil.format(new DateTime().plusDays(day).toDate(), "MM-dd");
		SqlPara sqlPara = Db.use("jiahua").getSqlPara("JHBirthStaff", date);
		List<JhStaff> staffs = JhStaff.dao.find(sqlPara);
		LogKit.info("生日人数 " + staffs.size());
		List<JhStaff> notiStaffs = JhStaff.dao.findByType(JhStaff.TYPE_PD);
		LogKit.info("通知人数: " + notiStaffs.size());
		for (JhStaff jhStaff : staffs) {
			for (JhStaff record : notiStaffs) {
				String content = "{}天后是{}的生日 {}";
				JhStaff staff = JhStaff.dao.findById(jhStaff.getId());
				String birth = DateUtils.dateFormat(staff.getBirth(), DateUtils.YYYYMMDD);
				content = StrFormatter.format(content, day, staff.getName(), birth);
				String title = "生日提醒";
				MessageUtils.send(record.getId(), null, title, content);
			}
		}
	}
}
